<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="110px" @submit.native.prevent>
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="pram.name" maxlength="100" placeholder="设备名称" />
      </el-form-item>
      <el-form-item label="设备品牌" prop="brand">
        <el-select v-model="pram.brand" placeholder="设备品牌" clearable filterable allow-create default-first-option @change="onBrandChange($event)" style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.brandList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备型号" prop="device">
        <el-select v-model="pram.device" placeholder="设备型号" clearable filterable allow-create default-first-option @change="onDeviceChange($event)" style="width: 100%">
          <el-option
            v-for="(item,index) in deviceList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="pram.deviceType" placeholder="设备类型" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.deviceTypeList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <template v-if="pram.deviceType === 57">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="卡1手机号" prop="mobile">
              <el-input v-model="pram.mobile" maxlength="100" placeholder="手机号（卡1）" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡2手机号" prop="otherMobile">
              <el-input v-model="pram.otherMobile" maxlength="100" placeholder="手机号（卡2）" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="机身串码1" prop="serialization">
              <el-input v-model="pram.serialization" maxlength="100" placeholder="机身串码1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机身串码2" prop="otherSerialization">
              <el-input v-model="pram.otherSerialization" maxlength="100" placeholder="机身串码2" />
            </el-form-item>
          </el-col>
        </el-row>
      </template>
      <template v-else>
        <el-form-item label="机身串码" prop="serialization">
          <el-input v-model="pram.serialization" maxlength="100" placeholder="机身串码" />
        </el-form-item>
      </template>
      <el-form-item label="所属人" prop="personId">
        <el-select v-model="pram.personId" placeholder="所属人" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.personList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Mac地址" prop="macAddress">
        <el-input v-model="pram.macAddress" maxlength="100" placeholder="Mac地址" />
      </el-form-item>
      <el-form-item label="蓝牙通讯地址" prop="bluetoothAddress">
        <el-input v-model="pram.bluetoothAddress" maxlength="100" placeholder="蓝牙通讯地址" />
      </el-form-item>
      <el-form-item label="启用时间" prop="activationTime">
        <el-date-picker v-model="pram.activationTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small" type="date" placement="bottom-end" style="width: 100%;" @change="onchangeSTime" placeholder="启用时间"/>
      </el-form-item>
      <el-form-item label="停用时间" prop="downTime">
        <el-date-picker v-model="pram.downTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small" type="date" placement="bottom-end" style="width: 100%;" @change="onchangeETime" placeholder="停用时间"/>
      </el-form-item>
      <el-form-item label="序号">
        <el-input-number v-model="pram.number" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:electronic:update','admin:electronic:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import * as electronicApi from '@/api/electronic.js'
import {Debounce} from '@/utils/validate'
import {elcAdd} from "@/api/electronic.js";
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allCategoryData:{
      type: Object
    }
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        name: null,
        brand: null, //默认1
        device: null,
        deviceType: null,
        serialization: null,
        macAddress: null,
        personId: null,
        mobile:null,
        otherMobile:null,
        otherSerialization:null,
        activationTime:null,
        downTime:null,
        bluetoothAddress:null,
        number:0,
        id: 0,
      },
      rules: {
        name: [{ required: true, message: '请填设备名称', trigger: ['blur', 'change'] }],
        brand: [{ required: true, message: '请选择设备品牌', trigger: ['blur', 'change'] }],
        device: [{ required: true, message: '请选择设备型号', trigger: ['blur', 'change'] }],
        deviceType: [{ required: true, message: '请选择设备类型', trigger: ['blur', 'change'] }],
        personId: [ { required: true, message: '请选择所属人', trigger: ['blur', 'change']} ],
        serialization: [ { required: true, message: '请填机身串码', trigger: ['blur', 'change']} ],
        mobile: [ { required: true, message: '请填手机号', trigger: ['blur', 'change']} ],
        macAddress: [ { required: true, message: '请填Mac地址', trigger: ['blur', 'change']} ],
        bluetoothAddress: [ { required: true, message: '请填蓝牙通讯地址', trigger: ['blur', 'change']} ],
      },
      categoryData:{
        brandList:[],
        deviceList:[],
        deviceTypeList:[],
        personList:[]
      },
      deviceList:[],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    onchangeSTime (e) {
      this.pram.activationTime = e;
    },
    onchangeETime (e) {
      this.pram.downTime = e;
    },
    onBrandChange(e) {
      if (e) {
        this.pram.brand = e
        this.pram.device = null //初始化选中值
        this.deviceList = this.categoryData.deviceList.filter(item=>item.pid===this.pram.brand)
        // 如果是对象，要使用this.$set方法
        // this.$set('typeObj', 'name', e.target.value)
      }
    },
    onDeviceChange(e) {
      if (e) {
        this.pram.device = e
        // 如果是对象，要使用this.$set方法
        // this.$set('typeObj', 'name', e.target.value)
      }
    },
    initEditData() {
      this.categoryData = this.allCategoryData

      if (this.isCreate !== 1) return //编辑
      const { name, brand, device, deviceType, serialization, otherSerialization, macAddress, personId, mobile, otherMobile, activationTime, downTime, bluetoothAddress, id ,number} = this.editData
      this.pram.name = name
      this.pram.brand = brand
      this.pram.device = device
      this.pram.deviceType = deviceType
      this.pram.serialization = serialization
      this.pram.otherSerialization = otherSerialization
      this.pram.macAddress = macAddress
      this.pram.personId = personId
      this.pram.mobile = mobile
      this.pram.otherMobile = otherMobile
      this.pram.activationTime = activationTime
      this.pram.downTime = downTime
      this.pram.bluetoothAddress = bluetoothAddress
      this.pram.id = id
      this.pram.number = number

      this.deviceList = this.categoryData.deviceList.filter(item=>item.pid===this.pram.brand)
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      electronicApi.elcAdd(this.pram).then(data => {
        this.$message.success('创建设备成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      electronicApi.elcAdd(this.pram).then(data => {
        this.$message.success('更新设备成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
